﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Abp;
using Abp.Localization;
using Abp.Notifications;
using Microsoft.Extensions.Configuration;
using pandx.Mulan.Authorization.Users;
using pandx.Mulan.Configuration;

namespace pandx.Mulan.Notifications
{
    public class AppNotifier:MulanDomainServiceBase,IAppNotifier
    {
        private readonly INotificationPublisher _notificationPublisher;
        private readonly IConfigurationRoot _configuration;
        public AppNotifier(INotificationPublisher notificationPublisher,IAppConfigurationAccessor configurationAccessor)
        {
            _notificationPublisher = notificationPublisher;
            _configuration = configurationAccessor.Configuration;
        }

        public async Task SendSimpleMessageAsync(UserIdentifier user, string message,
            NotificationSeverity severity = NotificationSeverity.Info)
        {
            await _notificationPublisher.PublishAsync(AppNotifications.SimpleMessage,
                new MessageNotificationData(message), severity: severity, userIds: new[] { user });
        }

        public Task SendCommonMessageAsync(UserIdentifier user, string title, string content,
            NotificationSeverity severity = NotificationSeverity.Info)
        {
            return SendNotificationAsync(AppNotifications.CommonMessage, user, new Dictionary<string, object>
            {
                {"title",title},{"content",content}
            });
        }

        public Task SendLocalizableMessageAsync(UserIdentifier user, LocalizableString localizableMessage,
            IDictionary<string, object> localizableMessageData = null,
            NotificationSeverity severity = NotificationSeverity.Info)
        {
            return SendNotificationAsync(AppNotifications.LocalizableMessage, user, localizableMessage,
                localizableMessageData, severity);
        }

        public Task SendCommonMessageAsync(UserIdentifier user, IDictionary<string, object> messageData = null,
            NotificationSeverity severity = NotificationSeverity.Info)
        {
            return SendNotificationAsync(AppNotifications.CommonMessage, user, messageData, severity);
        }

        private async Task SendNotificationAsync(string notificationName, UserIdentifier user,
            LocalizableString localizableMessage, IDictionary<string, object> localizableMessageData = null,
            NotificationSeverity severity = NotificationSeverity.Info)
        {
            var notificationData = new LocalizableMessageNotificationData(localizableMessage);
            if (localizableMessageData != null)
            {
                foreach (var pair in localizableMessageData)
                {
                    notificationData[pair.Key] = pair.Value;
                }
            }
            
            await _notificationPublisher.PublishAsync(notificationName, notificationData, severity:severity, userIds:new[] { user });
        }

        private async Task SendNotificationAsync(string notificationName, UserIdentifier user,
            IDictionary<string, object> messageData = null, NotificationSeverity severity = NotificationSeverity.Info)
        {
            var notificationData = new NotificationData();
            if (messageData != null)
            {
                foreach (var pair in messageData)
                {
                    notificationData[pair.Key] = pair.Value;
                }
            }

            await _notificationPublisher.PublishAsync(notificationName, notificationData, severity: severity,
                userIds: new[] { user });
        }
        
        private async Task SendNotificationAsync(string notificationName, UserIdentifier[] users,
            IDictionary<string, object> messageData = null, NotificationSeverity severity = NotificationSeverity.Info)
        {
            var notificationData = new NotificationData();
            if (messageData != null)
            {
                foreach (var pair in messageData)
                {
                    notificationData[pair.Key] = pair.Value;
                }
            }

            await _notificationPublisher.PublishAsync(notificationName, notificationData, severity: severity,
                userIds: users);
        }
        
        public async Task WelcomeToTheApplicationAsync(UserIdentifier user)
        {
            await SendCommonMessageAsync(user, "欢迎访问" + _configuration["App:AppName"],
                "欢迎访问" + _configuration["App:AppName"] + "，在使用过程中如有问题，请加QQ群：630281431讨论。", NotificationSeverity.Info);
        }

        public Task SomeUsersCannotBeImported(UserIdentifier user, string fileToken, string fileType, string fileName)
        {
            return SendNotificationAsync(AppNotifications.DownloadInvalidImportUsers, user,
                new Dictionary<string, object>
                {
                    {"fileToken",fileToken},
                    {"fileType",fileType},
                    {"fileName",fileName}
                });
        }
        public Task HaveToCopy(UserIdentifier[] users, long flowInstanceId,string flowInstanceName,string name)
        {
            return SendNotificationAsync(AppNotifications.HaveToCopy, users, new Dictionary<string, object>
            {
                {"flowInstanceId",flowInstanceId},
                {"name",name},
                {"flowInstanceName",flowInstanceName}
            });
        }
        public Task HaveToVerify(UserIdentifier user,long flowInstanceId,string flowInstanceName,string name)
        {
            return SendNotificationAsync(AppNotifications.HaveToVerify, user, new Dictionary<string, object>
            {
                {"flowInstanceId",flowInstanceId},
                {"name",name},
                {"flowInstanceName",flowInstanceName}
            });
        }
        public Task NeedToVerify(UserIdentifier[] users, long flowInstanceId,string flowInstanceName,string name)
        {
            return SendNotificationAsync(AppNotifications.NeedToVerify, users, new Dictionary<string, object>
            {
                {"flowInstanceId",flowInstanceId},
                {"name",name},
                {"flowInstanceName",flowInstanceName}
            });
        }

        public Task ConcurrentLoginPerUser(UserIdentifier user, string remoteIpAddress,string name,string dateTime)
        {
            return SendNotificationAsync(AppNotifications.ConcurrentLoginPerUser, user,
                new Dictionary<string, object>
                {
                    {"remoteIpAddress",remoteIpAddress},
                    {"name",name},
                    {"dateTime",dateTime}
                });
        }
    }
}